package cn.itcast.order.service;

import cn.itcast.feign.client.UserClient;
import cn.itcast.feign.pojo.User;
import cn.itcast.order.mapper.OrderMapper;
import cn.itcast.order.pojo.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author wl
 */
@Service
public class OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserClient userClient;

//    @Autowired
//    private RestTemplate restTemplate;
//
//    public Order queryOrderById(Long orderId) {
//        // 1.查询订单
//        Order order = orderMapper.findById(orderId);
//        /*
//          2.利用RestTemplate函数发起HTTP请求，查询用户信息
//          通过URL路径，发送HTTP请求，实现远程调用
//         */
//        final String url = "http://userService/user/" + order.getUserId();
//
//        User user = restTemplate.getForObject(url, User.class);
//        // 3.封装user到order中
//        order.setUser(user);
//        // 4.返回
//        return order;
//    }

    public Order queryOrderById(Long orderId) {
        // 1.查询订单
        Order order = orderMapper.findById(orderId);
        // 2.使用Feign
        User user = userClient.findById(order.getUserId());
        // 3.封装user到order中
        order.setUser(user);
        // 4.返回
        return order;
    }

}
